<?php

// setup Zend auto loader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$url      = 'http://soa:8001/SsoWS/api/xmlrpc';
$username = 'cesar.miquel@gmail.com';
$password = 'qwerty';

$client = new Zend_XmlRpc_Client( $url );


try {

	// me logeo
	echo "Login in...\n";
	$result = $client->call( 'login', array($username, $password) );
	if ( $result === false ) {
		die( "Usuario o contraseña invalido." );
	}

	// traigo las info del user
	echo "User info:\n";
	$userInfo = $client->call( 'getInfo', array( $username ) );
	print_r($userInfo);
	$token = $result['token'];

	echo "User logged. Token: $token\n";

	// traigo la credencial
	$credential = $client->call( 'getCredential', array($token) );
	echo "Credential: $credential\n";

	// traigo el user id
	$userId = $client->call( 'getUserIdByUsername', array( $username ) );
	echo "userId: $userId\n";

	// borro todas las props
	$stat = $client->call( 
		'clearProperties', 
		array( 
			(int) $userId, 
			$credential 
		) 
	);

	// actualizo props
	$newProperties = array(
		'first_name' => 'Cesar',
		'last_name' => 'Miquel',
		'prop3' => 'value3',
	);
	$stat = $client->call( 
		'updateUserProperties', 
		array( 
			(int) $userId, 
			$credential, 
			$newProperties 
		) 
	);

	// traigo las info del user
	$userInfo = $client->call( 'getInfo', array( $username ) );
	print_r($userInfo);

	echo "Changing password...\n";
	$stat = $client->call( 
		'changePassword', 
		array( 
			(int) $userId, 
			$credential, 
			'abcdefghi'
		) 
	);

	// me vuelvo a logear
	echo "Login in...\n";
	$result = $client->call( 'login', array($username, 'abcdefghi') );
	if ( $result === false ) {
		die( "Usuario o contraseña invalido." );
	}

	// vuelvo la contraseña atrás
	echo "Resetting password...\n";
	$stat = $client->call( 
		'changePassword', 
		array( 
			(int) $userId, 
			$credential, 
			$password
		) 
	);

	// me vuelvo a logear
	echo "Login in...\n";
	$result = $client->call( 'login', array($username, $password) );
	if ( $result === false ) {
		die( "Usuario o contraseña invalido." );
	}

	// traigo las info del user
	echo "User info:\n";
	$userInfo = $client->call( 'getInfo', array( $username ) );
	print_r($userInfo);


} catch( Exception $e ) {
	print( $e->getMessage() );
}
